﻿using _2Net8Configuration.Applications;
using _2Net8Configuration.Customer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace _2Net8Configuration
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            #region 1\ Json 文件
            {
                //ConfigurationBuilder builder = new ConfigurationBuilder();
                ////builder.AddJsonFile("AppSettings.json");
                //builder.AddJsonFile("AppSettings.json");
                //IConfiguration configuration = builder.Build();
                //var appName = configuration["AppSettings:ApplicationName"];
                //var version = configuration["AppSettings:Version"];
            }
            #endregion

            #region XML 文件
            //{
            //    ConfigurationBuilder builder = new ConfigurationBuilder();
            //    //builder.AddJsonFile("AppSettings.json");
            //    builder.AddXmlFile("AppSettings.xml");
            //    IConfiguration configuration = builder.Build();
            //    string appName = configuration["AppSettings:ApplicationName"];
            //    string version = configuration["AppSettings:Version"];
            //}
            #endregion

            #region 3 yaml 文件
            //{
            //    ConfigurationBuilder builder = new ConfigurationBuilder();
            //    //builder.AddJsonFile("AppSettings.json");
            //    builder.AddYamlFile("AppSettings.yaml");
            //    IConfiguration configuration = builder.Build();
            //    string appName = configuration["AppSettings:ApplicationName"];
            //    string version = configuration["AppSettings:Version"];
            //}
            #endregion
            #region 4\ 命令行
            //{
            //    ConfigurationBuilder builder = new ConfigurationBuilder();
            //    builder.AddCommandLine(args);
            //    IConfiguration configuration = builder.Build();
            //    string appName = configuration["ApplicationName"];
            //    string version = configuration["Version"];

            //    Console.WriteLine(appName);
            //    Console.WriteLine(version);
            //}
            #endregion

            #region 5、 自定义配置文件
            //{
            //    ConfigurationBuilder builder = new ConfigurationBuilder();
            //    TextConfigurationSource textConfiguration = new TextConfigurationSource("AppSettings.txt");
            //    builder.Add(textConfiguration);
            //    IConfiguration configuration = builder.Build();

            //    // 读取配置
            //    string appName = configuration["AppSettings.ApplicationName"];
            //    string version = configuration["AppSettings.Version"];

            //    Console.WriteLine(appName);
            //    Console.WriteLine(version);
            //}
            #endregion

            #region 6、数据绑定
            {
                //// 1、构造配置对象
                //ConfigurationBuilder builder = new ConfigurationBuilder();
                //builder.AddJsonFile("AppSettings.json");
                //IConfiguration configuration = builder.Build();

                //// 2、数据绑定
                //AppSettings appSettings = new AppSettings();
                //configuration.GetSection("AppSettings").Bind(appSettings);

                //// 3、读取配置信息
                //Console.WriteLine($"ApplicationName:{appSettings.ApplicationName}");
                //Console.WriteLine($"Version:{appSettings.Version}");
            }
            #endregion

            #region 7、配置选项
            //{
            //    // 1、构造配置对象
            //    ConfigurationBuilder builder = new ConfigurationBuilder();
            //    builder.AddJsonFile("AppSettings.json");
            //    IConfiguration configuration = builder.Build();

            //    // 2、配置绑定 + 配置对象注册
            //    ServiceCollection services = new ServiceCollection();
            //    services.Configure<AppSettings>(configuration.GetSection("AppSettings"));

            //    // 3、读取配置信息IOptions
            //    var serviceProvider = services.BuildServiceProvider();
            //    IOptions<AppSettings> options = serviceProvider.GetRequiredService<IOptions<AppSettings>>();
            //    AppSettings appSettings = options.Value;
            //    // 3、读取配置信息
            //    Console.WriteLine($"ApplicationName:{appSettings.ApplicationName}");
            //    Console.WriteLine($"Version:{appSettings.Version}");
            //}
            #endregion

            #region 8、配置应用
            {
                // 1、构造配置对象
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddJsonFile("AppSettings.json");
                IConfiguration configuration = builder.Build();

                // 2、配置绑定 + 配置对象注册
                ServiceCollection services = new ServiceCollection();
                services.Configure<ProductServiceSettings>(configuration.GetSection("ProductServiceSettings"));
                services.AddSingleton<ProductServices>();
                // 3、读取配置信息IOptions
                var serviceProvider = services.BuildServiceProvider();
                ProductServices productServices = serviceProvider.GetRequiredService<ProductServices>();
                productServices.GetProduct();

            }
            #endregion
        }
    }
}
